//
//  ExtensionViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/18.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation
import UIKit

private extension Double {
    var km: Double { return self * 1_000.0 }
    var m : Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}

private struct Point {
    var x = 0.0, y = 0.0
}

private struct Size {
    var width = 0.0, height = 0.0
}

private struct Rect {
    var point = Point()
    var size = Size()
}

private extension Rect {
    init(size:Size,center:Point){
        self.point = center
        self.size = size
    }
}

extension Array {
    fileprivate func readCount(){
        if !self.isEmpty {
            print("I have " + String(self.count) + " subjects")
        }
    }
}

private class XiaoMing {
    var name : String
    let height = 1.5
    init(name : String){
        self.name = name
    }
}

extension XiaoMing {
    func run() {
        print("xiaoming can run")
    }
    
    enum Body {
        case short,tall,other
    }
    
    var body : Body {
        switch self.height {
        case 1.5,1.6:
            return .short
        case 1.7,1.8:
            return .tall
        default:
            return .other
        }
    }
}

class ExtensionViewController : BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titles = ["computedProperties","initializers","methods","nestedTypes"]
        self.tableView?.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        switch indexPath.row{
        case 0:
            computedProperties()
        case 1:
            initializers()
        case 2:
            methods()
        case 3:
            nestedTypes()
        default:
            print("no more methods")
        }
    }
    
    fileprivate func computedProperties(){
        let oneInch = 25.4.mm
        print("One inch is \(oneInch) meters")
        let threeFeet = 3.ft
        print("Three feet is \(threeFeet) meters")
    }
    
    fileprivate func initializers(){
        let rect = Rect.init(size: Size.init(width: 100, height: 100), center: Point.init(x: 10, y: 20))
        print(rect)
    }
    
    fileprivate func methods(){
        let array = [1,2,3,4]
        array.readCount()
    }
    
    fileprivate func nestedTypes(){
        let xiaoming = XiaoMing.init(name: "xiaoming")
        xiaoming.run()
        
        switch xiaoming.body {
        case .short:
            print("xiaoming is short")
        case .tall:
            print("xiaoming is tall")
        case .other:
            print("xiaoming is monster")
        }
        
    }
}
